import { getObjectsByPrototype } from 'game/utils';
import { Creep, StructureContainer, StructureSpawn } from 'game/prototypes';


export class UnitManager {

    /**
     * @type {Unit}
     */
    myUnit;

    /**
     * @type {Unit}
     */
    enemyUnit;


    loop() {
        var myCreeps = getObjectsByPrototype(Creep).filter(creep => creep.my);
        // TODO 这里可能还没制造完成
        var enemyCreeps = getObjectsByPrototype(Creep).filter(creep => !creep.my);
        var mySpawn = getObjectsByPrototype(StructureSpawn).find(spawn => spawn.my);
        var enemySpawn = getObjectsByPrototype(StructureSpawn).find(spawn => !spawn.my);

        // 区分敌我资源
        var constants = getObjectsByPrototype(StructureContainer);
        var myConstants = [];
        var enemyConstants = [];

        constants.forEach(constant => {
            if (Math.abs(constant.x - mySpawn.x) < Math.abs(constant.x - enemySpawn.x)) {
                // 里自己的工厂更近
                myConstants.push(constant);
            } else {
                enemyConstants.push(constant);
            }
        });

        var myUnit = new Unit();
        var enemyUnit = new Unit();

        myUnit.creeps = myCreeps;
        enemyUnit.creeps = enemyCreeps;
        myUnit.spawn = mySpawn;
        enemyUnit.spawn = enemySpawn;
        myUnit.constants = myConstants;
        enemyUnit.constants = enemyConstants;
        this.myUnit = myUnit;
        this.enemyUnit = enemyUnit;
    }
}

class Unit {
    /**
     * @type {StructureSpawn}
     */
    spawn;

    /**
     * @type {Creep[]}
     */
    creeps = [];

    /**
     * @type {StructureContainer[]}
     */
    constants = [];
}